package com.example.myapplication;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {
    private Button startButton;
    private ImageView pointView;
    private int count = 1;
    private Handler handler = new Handler();
    private int centerX;
    private int centerY;
    private int right;
    private int down;

    // 假设你已经获取了以下值：
//    float screenPhysicalWidthInInches = ...; // 屏幕的物理宽度（英寸）
//    float screenPhysicalHeightInInches = ...; // 屏幕的物理高度（英寸）
//    int screenWidthInPixels = ...; // 屏幕的像素宽度
//    int screenHeightInPixels = ...; // 屏幕的像素高度

    double screenPhysicalWidthInInches = 2.71; // 屏幕的物理宽度（英寸）
    double screenPhysicalHeightInInches = 6.02; // 屏幕的物理高度（英寸）
    int screenWidthInPixels = 1080; // 屏幕的像素宽度
    int screenHeightInPixels = 2400; // 屏幕的像素高度

    double[] convertPixelsToCm(int x, int y) {
        // 计算每英寸的像素数量（像素密度）
        double CMperInchWidth = screenPhysicalWidthInInches * 2.54 / screenWidthInPixels;
        double CMperInchHeight = screenPhysicalHeightInInches * 2.54 / screenHeightInPixels;

        // 转换像素坐标为厘米坐标
        double cmX = x * CMperInchHeight; // 像素转换为厘米
        double cmY = y * CMperInchWidth; // 像素转换为厘米

        // 将厘米坐标作为数组返回
        return new double[]{cmX, cmY};
    }
    // 声明像素坐标转换为厘米坐标的函数
//    private float[] convertPixelsToCm(int x, int y) {
//        // 计算每英寸的像素数量（像素密度）
//        float pixelsPerInchX = screenWidthInPixels / screenPhysicalWidthInInches;
//        float pixelsPerInchY = screenHeightInPixels / screenPhysicalHeightInInches;
//
//        // 转换像素坐标为厘米坐标
//        float cmX = x / pixelsPerInchX * 2.54f; // 像素转换为厘米
//        float cmY = y / pixelsPerInchY * 2.54f; // 像素转换为厘米
//
//        // 将厘米坐标作为数组返回
//        return new float[]{cmX, cmY};
//    }
//    int x = (width - p_w) / 2; // 假设这是像素坐标
//    int y = (height - p_h * 2) / 2; // 假设这是像素坐标
//
//    float[] cmCoordinates = convertPixelsToCm(x, y);
//
//    float cmX = cmCoordinates[0]; // 厘米 X 坐标
//    float cmY = cmCoordinates[1]; // 厘米 Y 坐标
//  保存一下 得修改重写大部分了

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        startButton = findViewById(R.id.startButton);
        pointView = findViewById(R.id.pointView);

        ViewGroup.LayoutParams layoutParams = pointView.getLayoutParams();

        layoutParams.width *= 2;
        layoutParams.height *= 2;

        pointView.setLayoutParams(layoutParams);


//        // 设置初始点的位置
//        pointView.setX(centerX);
//        pointView.setY(centerY);

        startButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 开始循环显示和隐藏点
                handler.post(showHidePointRunnable);
            }
        });
    }

    private Runnable showHidePointRunnable = new Runnable() {
        @Override
        public void run() {
            // 计算点出现在屏幕中间的位置
            int width = getWindow().getDecorView().getWidth();
            int height = getWindow().getDecorView().getHeight();
            int p_w = pointView.getWidth();
            int p_h = pointView.getHeight();
            right = width - p_w;
            down = height - p_h * 2;
            centerX = (width - p_w) / 2;
            centerY = (height - p_h * 2) / 2;
            int segmentWidth = width / 3;   //right width
            int segmentHeight = down / 5;   //down  height
//            int[] pointX = {0, 0, 0, 0, 0, segmentWidth, segmentWidth, segmentWidth, segmentWidth, segmentWidth * 2, segmentWidth * 2, segmentWidth * 2, segmentWidth * 2, segmentWidth * 2, 0};
//            int[] pointY = {0, segmentHeight, segmentHeight * 2, segmentHeight * 3, segmentHeight * 4, 0, segmentHeight, segmentHeight * 2, segmentHeight * 3, 0, segmentHeight, segmentHeight * 2, segmentHeight * 3, segmentHeight * 4, segmentHeight * 4};
            int[] pointX = {0, segmentWidth, segmentWidth * 2, 0, segmentWidth, segmentWidth * 2,0, segmentWidth, segmentWidth * 2,0, segmentWidth, segmentWidth * 2,0, segmentWidth, segmentWidth * 2};
            int[] pointY = {0, 0, 0, segmentHeight, segmentHeight, segmentHeight,segmentHeight * 2, segmentHeight * 2,segmentHeight * 2,segmentHeight * 3, segmentHeight * 3,segmentHeight * 3,segmentHeight * 4, segmentHeight * 4,segmentHeight * 4};
            int[] newOrder = {1, 12, 3, 5, 2, 11, 15, 7, 13, 4, 10, 6, 9, 8, 14};

            if(count <=15){
                int newIndex = newOrder[count - 1] - 1; // 获取新顺序数组中的下标
                int x = pointX[newIndex];
                int y = pointY[newIndex];

                // 将像素坐标转换为厘米坐标
                double[] cmCoordinates = convertPixelsToCm(x, y);
                double cmX = cmCoordinates[0];
                double cmY = cmCoordinates[1];

                // 输出厘米坐标
                System.out.println("X坐标（厘米）: " + cmX);
                System.out.println("Y坐标（厘米）: " + cmY);






                pointView.setX(x);
                pointView.setY(y);

                pointView.setVisibility(View.VISIBLE);
            } else{
                pointView.setVisibility(View.INVISIBLE);
            }

            count++;

            // 循环显示和隐藏点，每次间隔1秒
            if (count <= 15) {
                handler.postDelayed(this, 2000);
            }
        }
    };
}